package cn.peach.direct;

import com.oracle.jrockit.jfr.Producer;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;



public class Consumer1 {
    public static final String CONSUMER_FANOUT_ONE = "CONSUMER_FANOUT_ONE";
    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("115.159.88.63");

        Connection connection = factory.newConnection();
        // 创建通道
        Channel channel = connection.createChannel();
        channel.basicQos(1);
        // 创建队列
        channel.queueDeclare(CONSUMER_FANOUT_ONE,true,false,false,null);
        // 绑定队列到交换机
        channel.queueBind(CONSUMER_FANOUT_ONE, producer.EXCHANGE_DIRECT_ONE, "error");
        channel.queueBind(CONSUMER_FANOUT_ONE, producer.EXCHANGE_DIRECT_ONE, "opp");
        /*消息的回调处理*/
        // 消费者发送消
        Consumer callback = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                        AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("拿到消息："+new String(body));
                System.out.println("consumerTag："+consumerTag);
                System.out.println("交换机："+envelope.getExchange());
                System.out.println("队列："+envelope.getRoutingKey());
                System.out.println("消息的id（秘密文件）："+envelope.getDeliveryTag());

                // 拿到消息的id，手动签收
                channel.basicAck(envelope.getDeliveryTag(),
                        // 不要多个签收
                        false);
            }
        };

        // 监听队列A
        channel.basicConsume(CONSUMER_FANOUT_ONE,
                false,
                callback);


    }
}
